package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import logic.CRUDUsers;
import logic.Teacher;
import logic.User;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class EditAccount extends JFrame {

	private JPanel contentPane;
	private JTextField fieldName;
	private JTextField fieldRoom;
	private JPasswordField passField;
	private JPasswordField rptPassField;
	private JTextField fieldLastName;

	

	/**
	 * Create the frame.
	 */
	public EditAccount(final User u) {
		setTitle("Editar informações da conta - Coolrent");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 354, 225);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNome = new JLabel("Nome: ");
		lblNome.setBounds(49, 11, 46, 14);
		contentPane.add(lblNome);
		
		fieldName = new JTextField(u.getFirstName());
		fieldName.setBounds(155, 6, 136, 24);
		contentPane.add(fieldName);
		fieldName.setColumns(10);
		
		JLabel lblSala = new JLabel("Sala:");
		lblSala.setBounds(49, 61, 46, 14);
		contentPane.add(lblSala);
		
		fieldRoom = new JTextField(((Teacher) u).getSala());
		fieldRoom.setColumns(10);
		fieldRoom.setBounds(155, 56, 136, 24);
		contentPane.add(fieldRoom);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(49, 89, 46, 14);
		contentPane.add(lblSenha);
		
		passField = new JPasswordField();
		passField.setColumns(10);
		passField.setBounds(155, 84, 136, 24);
		contentPane.add(passField);
		
		JLabel lblRepitaASenha = new JLabel("Repita a Senha: ");
		lblRepitaASenha.setBounds(49, 116, 97, 14);
		contentPane.add(lblRepitaASenha);
		
		rptPassField = new JPasswordField();
		rptPassField.setColumns(10);
		rptPassField.setBounds(155, 111, 136, 24);
		contentPane.add(rptPassField);
		
		JLabel lblSobrenome = new JLabel("Sobrenome:");
		lblSobrenome.setBounds(49, 36, 79, 14);
		contentPane.add(lblSobrenome);
		
		fieldLastName = new JTextField(u.getLastName());
		fieldLastName.setColumns(10);
		fieldLastName.setBounds(155, 31, 136, 24);
		contentPane.add(fieldLastName);
		
		JButton btnNewButton = new JButton("Atualizar");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				fieldName.setText(fieldName.getText().trim());
				fieldLastName.setText(fieldLastName.getText().trim());
				
				u.setFirstName(fieldName.getText());
				u.setLastName(fieldLastName.getText());
				
				((Teacher) u).setSala(fieldRoom.getText());
				
				if ( !new String(passField.getPassword()).equals(new String(rptPassField.getPassword()))){
					JOptionPane.showMessageDialog(null, "Senhas não conferem");
				} else {
					try {
						CRUDUsers.deleteUser(u.getEmail());
						CRUDUsers.addUser(u.getFirstName(), u.getLastName(), u.getEmail(), new String(passField.getPassword()), fieldRoom.getText());
						JOptionPane.showMessageDialog(null, "Dados atualizados com sucesso");
						setVisible(false);
						dispose();
					} catch (Exception e ){
						JOptionPane.showMessageDialog(null, "Algo deu errado ao salvar seu usuário. Erro e: "+ e.getMessage());
					}
				}
				
			}
		});
		btnNewButton.setBounds(49, 142, 242, 28);
		contentPane.add(btnNewButton);
		
		
	}
}
